package model;

import java.sql.Date;

import javax.swing.table.AbstractTableModel;

import dao.tables.MEMBRE;
import dao.tables.RESPONSABILITE;

public class JTableMembreModel extends AbstractTableModel {
    private static final long serialVersionUID = 2534199402037550177L;

    MEMBRE[] data = new MEMBRE[0];
    String[] titles = new String[0];

    public boolean isCellEditable(int row, int col) {
	if (col == 0) {
	    return false;
	} else {
	    return true;
	}
    }

    public JTableMembreModel() {
    }

    public JTableMembreModel(MEMBRE[] listMembre, String[] titles) {
	this.setData(listMembre);
	this.titles = titles;
    }

    @Override
    public int getColumnCount() {
	return titles.length;
    }

    @Override
    public int getRowCount() {
	return data.length;
    }

    @Override
    public Object getValueAt(int row, int col) {
	switch (col) {
	case 0:
	    return data[row].getId();
	case 1:
	    return data[row].getCivilite();
	case 2:
	    return data[row].getNom();
	case 3:
	    return data[row].getPrenom();
	case 4:
	    return data[row].getDateDeNaissance();
	case 5:
	    return data[row].getResponsabilite();
	case 6:
	    return data[row].getListInstrument();
	case 7:
	    return data[row].getListSpecialite();
	default:
	    return null;
	}
    }

    @Override
    public void setValueAt(Object obj, int row, int col) {
	switch (col) {
	case 0:
	    data[row].setId((Long) obj);
	    break;
	case 1:
	    data[row].setCivilite((String) obj);
	    break;
	case 2:
	    data[row].setNom((String) obj);
	    break;
	case 3:
	    data[row].setPrenom((String) obj);
	    break;
	case 4:
	    data[row].setDateDeNaissance((Date) obj);
	    break;
	case 5:
	    data[row].setResponsabilite((RESPONSABILITE) obj);
	    break;
	}
    }

    public MEMBRE getMembreAt(int row) {
	return data[row];
    }

    public void setMembreAt(MEMBRE obj, int row) {
	data[row] = (obj);
    }

    public void addMembre(MEMBRE membre) {
	MEMBRE[] dataTmp = new MEMBRE[data.length + 1];
	dataTmp[data.length] = membre;
	data = dataTmp;
	dataTmp = null;
	this.fireTableRowsInserted(data.length, data.length);
    }

    public void removeMembreAt(int row) {
	MEMBRE[] dataTmp = new MEMBRE[data.length - 1];
	for (int i = 0; i < data.length; i++) {
	    if (i < row)
		dataTmp[i] = data[i];
	    else if (i > row)
		dataTmp[i - 1] = data[i];
	}
	data = dataTmp;
	dataTmp = null;
	this.fireTableRowsDeleted(row - 1, row - 1);
    }

    public void refreshValues(MEMBRE[] listMembre) {
	this.data = listMembre;
	this.fireTableRowsInserted(data.length, data.length);
    }

    @Override
    public String getColumnName(int col) {
	return titles[col];
    }

    public void setData(MEMBRE[] membres) {
	data = membres;
	this.fireTableRowsInserted(data.length, data.length);
    }
}
